﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Uow;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using LiveShopvnext.Application.Caching;
using Microsoft.Extensions.Caching.Distributed;

namespace LiveShopvnext.Books
{
    public class BookAppService : CrudAppService<Book, BookDto, Guid, BookListDto, CreateUpdateBookDto>, IBookAppService
    {
        public IDistributedCache Cache { get; set; }
        private readonly IRepository<Book, Guid> _repository;
        public BookAppService(IRepository<Book, Guid> repository) : base(repository)
        {
            _repository = repository;
        }

        ////判断是否授权
        //[Authorize]
        //[AbpAuthorize("")]
        [Obsolete]
        protected override IQueryable<Book> CreateFilteredQuery(BookListDto input)
        {
            return base.CreateFilteredQuery(input).WhereIf(!string.IsNullOrEmpty(input.Name), b => b.Name.Contains(input.Name));
        }

        //[UnitOfWork]//工作单元
        public override Task<PagedResultDto<BookDto>> GetListAsync(BookListDto input)
        {

            return base.GetListAsync(input);
        }

        [HttpGet("GetBookList")]
        public async Task<IEnumerable<Book>> GetBookList(int Type)
        {
            var query = await _repository.GetListAsync();
            if (Type == 1)
            {
                return query.WhereIf(true, b => b.IsDinner.Equals(true));
            }
            if (Type == 2)
            {
                return query.WhereIf(true, b => b.IsRecommend.Equals(true));
            }
            if (Type == 3)
            {
                return query.WhereIf(true, b => b.IsPopularity.Equals(true));
            }
            return query;
        }
        [HttpGet("GetBookListByRedis")]
        public async Task<IEnumerable<Book>> GetBookListByRedis(int Type)
        {
            return await Cache.GetOrAddAsync("BookListByType" + Type,


                async () =>
                {

                    var query = await _repository.GetListAsync();
                    if (Type == 1)
                    {
                        return query.WhereIf(true, b => b.IsDinner.Equals(true));
                    }
                    if (Type == 2)
                    {
                        return query.WhereIf(true, b => b.IsRecommend.Equals(true));
                    }
                    if (Type == 3)
                    {
                        return query.WhereIf(true, b => b.IsPopularity.Equals(true));
                    }
                    return query;
                },



            60
            );
            //TODO:后续如果有需要可以加缓存降级，即如果缓存中不存在主页楼层的数据，不去数据库中读，直接返回默认的楼层商品

        }
        [HttpGet("GetBookByRedis")]
        public async Task<BookDto> GetBookByRedis(Guid id)
        {
            return await Cache.GetOrAddAsync("Book" + id, async () =>
            {
                try
                {

                    var book = await _repository.GetAsync(id);
                    return ObjectMapper.Map<Book, BookDto>(book);
                }
                catch (Exception e)
                {
                    //TODO:后续需要加缓存穿透处理
                    return new BookDto { message = e.Message};
                }
            },
            24 * 60
            );

        }
    }
}
